package cn.compose.online.biz;

import cn.compose.online.dao.UserTokenMapper;
import cn.compose.online.entity.UserToken;
import cn.compose.online.entity.UserTokenExample;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

/**
 * @ClassName UserToken
 * @Description 类描述
 * @Author hgm
 * @Date 2021/12/12 22:24
 * @Version 1.0
 **/
@Service
@Slf4j
public class UserTokenBiz {
    @Resource
    private UserTokenMapper userTokenMapper;


    public void addEmailToken(String email){
        UserToken userToken=new UserToken();
        userToken.setEmail(email);
        userToken.setStatus(0);
        userToken.setUserId(UUID.randomUUID().toString());
        userTokenMapper.insert(userToken);
    }

    public UserToken getEmailToken(String email){
        try {
            UserTokenExample userTokenExample = new UserTokenExample();
            userTokenExample.createCriteria().andEmailEqualTo(email);
            List<UserToken> userTokens = userTokenMapper.selectByExample(userTokenExample);
            if (CollectionUtils.isEmpty(userTokens)) {
                return null;
            }
            return userTokens.get(0);
        }catch (Exception e){
            log.error("getEmailToken error",e);
        }
        return null;
    }
}
